//
//  ViewController.swift
//  PhotoKitDemo
//
//  Created by 李煜 on 2024/1/24.
// https://www.jianshu.com/p/f5b3b75ef8f0
//https://blog.csdn.net/diaochuanzhi7826/article/details/101967485

import AddressBook
import Contacts
import Photos
import UIKit
class ViewController: UIViewController {
    private var cameraAllAsset: [PHAsset] = [] // 相册中所有照片
    private var customAllAsset: [PHAsset] = [] //

    var name: String? = nil

    override func viewDidLoad() {
        super.viewDidLoad()

        photo()
        addressBookAuthorization()
    }
}

extension ViewController {
    func photo() {
        PHPhotoLibrary.requestAuthorization { (status: PHAuthorizationStatus) in
            switch status {
                case .restricted:
                    print("当前用户无法更改该权限状态")
                case .authorized:
                    print("允许当前权限的请求")
                case .denied:
                    print("拒绝当前权限的请求")
                case .limited:
                    print("受限")

                default:
                    break
            }
        }
    }

    func addressBookAuthorization() {
        // ios 9
        if CNContactStore.authorizationStatus(for: .contacts) == .notDetermined {
            let store = CNContactStore()
            store.requestAccess(for: .contacts) { granted, error in
                if !granted {
                    print("未获得通讯录访问权限:\(String(describing: error))")
                    return
                }
                print("获得通讯录访问权限")
            }
        }
    }
}
